import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;

public class NinjaFish extends Fish
{
    private boolean isInvisible;
    private Random gen;
    private Fish prey, lee;
    
    public NinjaFish(Environment env, Location loc) {
        super(env, loc);
        gen = new Random();
    }

    public NinjaFish(Environment env, Location loc, Direction dir) {
        super(env, loc, dir);
        gen = new Random();
    }

    public NinjaFish(Environment env, Location loc, Direction dir, Color col) {
        super(env, loc, dir, col);
        gen = new Random();
    }
    
    public void dissapear() {
        environment().remove(this);
    }
    
    
    public void chooseVictim() {
        Locatable[] targets = environment().allObjects();
        int index = gen.nextInt(targets.length);
        prey = (Fish)targets[index];
    }
    
    protected Location nextLocation() {
        Location spot;
        if(prey.direction() == Direction.EAST) {
            spot = new Location((prey.location().col()) - 1, prey.location().row());
        }
        else if(prey.direction() == Direction.WEST) {
            spot = new Location((prey.location().col()) + 1, prey.location().row());
        }
        else if(prey.direction() == Direction.NORTH) {
            spot = new Location((prey.location().col()), prey.location().row() - 1);
        }
        else {
            spot = new Location((prey.location().col()), prey.location().row() + 1);
        }
        return spot;
    }
    
    public void killFish() {
        Location inFront = environment().getNeighbor(location(), direction());
        ((Fish)environment().objectAt(inFront)).die();
         
    }
    
    public void act() {
        while(environment().numObjects() != 0) {
            chooseVictim();
            dissapear();
            if(gen.nextInt(10) == 5) {
                environment().add(this);
                changeLocation(nextLocation());
                changeDirection(prey.direction());
                killFish();
            }
        }
    }    
    
}
